package com.ruoyi.wx.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wx.pojo.vo.VerificationVo;
import com.ruoyi.wx.service.WxAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/wx/auth")
public class WxAuthController {

    @Autowired
    private WxAuthService wxAuthService;

    @GetMapping("/verification")
    public String verification(VerificationVo verificationVo) {
        System.out.println(verificationVo.getEchostr());
        return verificationVo.getEchostr();
    }

    @PostMapping("/accesstoken")
    public AjaxResult getAccessToken(String code, String state) {

        return AjaxResult.success();
    }

    @GetMapping("/admin/accesstoken")
    public AjaxResult adminGetAccessToken() {
        String accessToken = wxAuthService.adminGetAccessToken();
        return AjaxResult.success("WxAccessToken", accessToken);
    }

    @GetMapping("/qr")
    public AjaxResult getQr() {
        Map<String, Object> serviceQr = wxAuthService.getQr("123");
        if (serviceQr == null) return AjaxResult.error("错误");
        return AjaxResult.success(serviceQr);
    }

    @GetMapping("/code")
    public AjaxResult getCode() {
        return AjaxResult.success();
    }

    @GetMapping("/isauth")
    public AjaxResult isAuth(HttpServletRequest request) {
        boolean auth = wxAuthService.isAuth(request);
        if (auth) return AjaxResult.success();
        return AjaxResult.error();
    }

    @PostMapping("/verification")
    public String verificationMessage(HttpServletRequest httpServletRequest) throws IOException {
        httpServletRequest.setCharacterEncoding("UTF-8");
        wxAuthService.verificationMessage(httpServletRequest);
        return "success";
    }
}
